using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace OdsControllerLib {
	#region Observer interfaces
	/// <summary>
	/// Implements the GOF observer pattern to the log system, so that the log window can receive updates of the master log
	/// The log subject
	/// </summary>
	public interface ILogSubject {
		void Attach(ILogObserver observer);
		void Detach(ILogObserver observer);

		bool NotifyLogClients(string message, LogCategories category, DateTime timestamp);
	}
	/// <summary>
	/// Implements the GOF observer pattern to the log system, so that the log window can receive updates of the master log
	/// The log observer
	/// </summary>
	public interface ILogObserver {
		bool AppendLogEntry(ILogSubject sender, string message, LogCategories category, DateTime timestamp);
	}
	#endregion Observer interfaces


	public class OdsMainLogObj : ILogSubject {
		OdsFileLogObj _logFile;
		OdsEventLogObj _logEvent;
		string _logText;
		OdsLogLevel _logLevel;

		#region observer
		ArrayList _observerClients = new ArrayList();

		public void Attach(ILogObserver client) {
			_observerClients.Add(client);
		}
		public void Detach(ILogObserver client) {
			_observerClients.Remove(client);
		}

		public bool NotifyLogClients(string message, LogCategories category, DateTime timestamp) {
			for (int i = 0; i < _observerClients.Count; i++)
				((ILogObserver)_observerClients[i]).AppendLogEntry(this, message, category, timestamp);

			return true;
		}
		#endregion observer


		public OdsMainLogObj(string logFilename, string eventLogName, string eventSourceName) {
			_logFile = new OdsFileLogObj();
			_logFile.LogFilename = logFilename;

			_logEvent = new OdsEventLogObj();
			_logEvent.EventLogName = eventLogName;
			_logEvent.EventLogSource = eventSourceName;
		}

		public OdsFileLogObj LogFile {
			get { return _logFile; }
			set { _logFile = value; }
		}
		public OdsEventLogObj LogEvent {
			get { return _logEvent; }
			set { _logEvent = value; }
		}
		public OdsLogLevel LogLevel {
			get { return _logLevel; }
			set { _logLevel = value; }
		}

		public void Write(string message, LogCategories category, DateTime timestamp) {
			//TODO: falta controlar os LogLevel

			switch (category) { 
				case LogCategories.UIActions:
					if ((int)_logLevel >= (int)OdsLogLevel.Normal) {
						NotifyLogClients(message, category, timestamp);
						LogFile.Write(message, category, timestamp);
					}
					break;
				case LogCategories.ApplicationEvents:
					if ((int)_logLevel >= (int)OdsLogLevel.Minimal) {
						NotifyLogClients(message, category, timestamp);
						LogFile.Write(message, category, timestamp);
					}
					break;
				case LogCategories.CommunicationsSent:
					if ((int)_logLevel >= (int)OdsLogLevel.Normal) {
						NotifyLogClients(message, category, timestamp);
						LogFile.Write(message, category, timestamp);
					}
					break;
				case LogCategories.CommunicationsReceived:
					if ((int)_logLevel >= (int)OdsLogLevel.Minimal) {
						NotifyLogClients(message, category, timestamp);
						LogFile.Write(message, category, timestamp);
					}
					break;
				case LogCategories.ServiceEvents:
					if ((int)_logLevel >= (int)OdsLogLevel.Minimal) {
						NotifyLogClients(message, category, timestamp);
						LogFile.Write(message, category, timestamp);
					}
					break;
			}
		}





		public string ReadLogFile() {
			throw new System.NotImplementedException();
		}

		public void Clear() {
			throw new System.NotImplementedException();
		}

		public void SaveLogFile() {
			throw new System.NotImplementedException();
		}



		public event EventHandler LogChanged;
	}
}
